"""
-- coding: utf-8 --
@Time : 2023/7/31
@Author : 张书译
@Describe:读取ClickHouse
"""

from clickhouse_sqlalchemy import make_session
from sqlalchemy import create_engine
import pandas as pd
from Comm.utils.ReadIniFile import ReadIniFile


class ConnectCK:

    def __init__(self, host=ReadIniFile().get_str('clickhouse', 'host'),
                 port=ReadIniFile().get_str('clickhouse', 'port'),
                 user=ReadIniFile().get_str('clickhouse', 'user'),
                 password=ReadIniFile().get_str('clickhouse', 'password'),
                 db=ReadIniFile().get_str('clickhouse', 'database'),
                 ):
        self.host = host
        self.port = port
        self.user = user
        self.password = password
        self.db = db

    def Link(self):
        cursor = f'clickhouse://{self.user}:{self.password}@{self.host}:{self.port}/{self.db}'
        engine = create_engine(cursor)
        return engine

    def select_clickhouse(self, sql, engine=None, look=None):
        if engine is None:
            engine = self.Link()
        # # 查询语句
        if look:
            pd.set_option('display.max_columns', None, 'display.width', None)
            pd.set_option('display.max_rows', None, 'display.width', None)
        query = sql

        data = pd.read_sql_query(query, engine)

        return data
